package com.isis.servlet;
import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.isis.jdo.crud.ManagerDataStore;
import com.isis.service.SendChatMessage;
import java.net.URLEncoder;

@SuppressWarnings("serial")
public class AuthentificationControl extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String emailAdmin="edaheron@gmail.com";
		if (user != null) { // L'utilisateur est déjà  authentifié
						
			ManagerDataStore mds= new ManagerDataStore();
			
			mds.addUserConnexion(user.getEmail());
			int nbConnexion =mds.countUserConnexion(user.getEmail());
			String repChatMessageControl=null;
			if((nbConnexion%10)==0){
				SendChatMessage chatMessage=new SendChatMessage(emailAdmin);
				repChatMessageControl = chatMessage.sendMessageToChat("L'utilisateur "+user.getEmail()+" viens de se connecter pour la "+nbConnexion+" fois à l'application ISISProject+");
				
			}
			if(repChatMessageControl==null){
				response.sendRedirect("/authentification");
			}else{
				response.sendRedirect("/authentification?repChatMessageControl="+URLEncoder.encode(repChatMessageControl, "UTF-8"));
			}
			
			
		}
	}
}
